<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI助手</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 20px;
        }
        .response {
            margin-top: 20px;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 8px;
            width: 50%;
            text-align: center;
        }
        .response p {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<div class="container">
    <div>
        <label for="textInput">输入文本：</label>
        <input type="text" id="textInput" style="padding: 10px; border-radius: 8px; border: 1px solid #ccc;">
        <button onclick="sendRequest()" style="padding: 10px; border-radius: 8px; border: 1px solid #ccc;">发送</button>
    </div>
    <div class="response" id="output">
        <!-- 这里显示后端返回的输出 -->
<!--        响应将在这里展示-->
    </div>
</div>

<script>
    function sendRequest() {
        var inputText = document.getElementById("textInput").value;
        var url = "http://localhost:8081/api/chat";

        fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json; utf-8",
                "Accept": "application/json"
            },
            body: JSON.stringify({
                query: inputText,
            })
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();  // 解析响应为JSON
        })
        .then(data => {
            // 处理并显示后端返回的数据
            var outputDiv = document.getElementById("output");
            var content = data.message.content;
<!--            助手回复头像 名称 回复内容 -->
            outputDiv.innerHTML = '<p><img src=" " alt=" " class="avatar"> 通义千问 ：' + content + '</p>';
        })
        .catch(error => {
            console.error('Error:', error);
            var outputDiv = document.getElementById("output");
            outputDiv.innerHTML = "<p>请求失败，请检查控制台日志。</p>";
        });
    }
</script>
</body>
</html>
